package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.adapter.ProcurementManagerAdapter;
import com.xca.znb.entity.Procurement;
import com.xca.znb.model.impl.ProcurementManagerImpl;
import com.xca.znb.presenter.ProcurementManagerPresenter;

import java.util.List;

public class MyProcurementActivity extends AppCompatActivity {
    private ImageView Back ;
    private ListView listView ;
    private List<Procurement> pList;
    private ProcurementManagerAdapter adapter ;
    private ProcurementManagerPresenter presenter ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_procurement_activity);
        Back = findViewById(R.id.my_procurement_back);
        listView = findViewById(R.id.my_procurement_goods_list);

        ProcurementManagerImpl procurementManager = new ProcurementManagerImpl(this);
        presenter = new ProcurementManagerPresenter(procurementManager);

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String login_phone = sharedPreferences.getString("phone" , "");

        pList = presenter.getProcurementByPhone(login_phone);

        adapter = new ProcurementManagerAdapter(this,pList);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.procurement_manager_id);
                String sId = showId.getText().toString() ;
                Intent intent = new Intent(MyProcurementActivity.this , ProcurementMessage.class);
                intent.putExtra("pManToMeId" , sId);
                intent.putExtra("sales_phone",login_phone);
                startActivity(intent);
            }
        });

        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String login_phone = sharedPreferences.getString("phone" , "");

        pList = presenter.getProcurementByPhone(login_phone);

        adapter = new ProcurementManagerAdapter(this,pList);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

}
